﻿//Get query string param value
$.extend({
    getQueryStringValue: function (paramName) {
        /// <summary>
        ///     Get the value of input parameter from the querystring
        /// </summary>
        /// <param name="paramName" type="String">The input parameter whose value is to be extracted</param>
        /// <returns type="String">The value of input parameter from the querystring</returns>

        parName = paramName.replace(/[\[]/, '\\\[').replace(/[\]]/, '\\\]');
        var pattern = '[\\?&]' + paramName + '=([^&#]*)';
        var regex = new RegExp(pattern);
        var matches = regex.exec(window.location.href);
        if (matches == null) return '';
        else return decodeURIComponent(matches[1].replace(/\+/g, ' '));
    }
});

//Global growl settings
$.extend($.gritter.options, {
    position: 'top-left', // defaults to 'top-right' but can be 'bottom-left', 'bottom-right', 'top-left', 'top-right' (added in 1.7.1)
    fade_in_speed: 500, // how fast notifications fade in (string or int)
    fade_out_speed: 1000, // how fast the notices fade out
    time: 5000 // hang on the screen for...
});

//Static type for growl message
function GrowlType() { };
GrowlType.ERROR = -1;
GrowlType.SUCCESS = 0;
GrowlType.INFO = 1;
GrowlType.WARNING = 2;
//Growl message extend
$.extend({
    showGrowlMessage: function (type, message) {

        var growlId = $.gritter.add({
            // (string | mandatory) the heading of the notification
            title: 'I am to be replaced :)',
            // (string | mandatory) the text inside the notification
            text: message
        });

        //Get the title of the growl message
        var growlTitle = $('#gritter-item-' + growlId.toString() + ' .gritter-title');

        switch (type) {
            case GrowlType.ERROR:
                growlTitle.text('Error!').css('color', 'red');
                break;
            case GrowlType.SUCCESS:
                growlTitle.text('Ok!').css('color', 'lightgreen');
                break;
            case GrowlType.INFO:
                growlTitle.text('Info:').css('color', 'white');
                break;
            case GrowlType.WARNING:
                growlTitle.text('Warning:').css('color', 'yellow');
                break;
        }
    }
});

//Growl sticky message extend
$.extend({
    showHelpMessage: function (message) {
        //Get multiple parts message
        var messages = message.split('|');
        //Build the help list
        var finalMsgList = '<ul style="margin-left:20px">';
        for (var i = 0; i < messages.length; i++) {
            finalMsgList += '<li>' + messages[i] + '</li>';
        }
        finalMsgList += '</ul>';
        //Then show the sticky growl
        $.gritter.add({
            // (string | mandatory) the heading of the notification
            title: 'Help',
            // (string | mandatory) the text inside the notification
            text: finalMsgList,
            sticky: true
        });
    }
});

//Whole screen block
$.extend({
    showLoading: function () {
        $.blockUI({
            message: "",
            css:
            {
                backgroundColor: 'transparent',
                border: 0,
                cursor: 'wait'
            },
            overlayCSS:
            {
                backgroundColor: '#fff',
                opacity: 0.5
            }
        });
    }
});


///////////////////////////////////////////////////
//               Custom plugins                  //
///////////////////////////////////////////////////
(function ($) {
    //Toggle required textbox mode
    $.fn.requiredModeOn = function () {
        this.animate({
            backgroundColor: "#FFA0A0",
            borderTopColor: "#FF3030",
            borderLeftColor: "#FF3030",
            borderRightColor: "#FF3030",
            borderBottomColor: "#FF3030"
        }, 500);
    };

    $.fn.requiredModeOff = function () {
        this.animate({
            backgroundColor: "#FFFFFF",
            borderTopColor: "#999999",
            borderLeftColor: "#999999",
            borderRightColor: "#999999",
            borderBottomColor: "#999999"
        }, 500);
    }

    //Fade in text
    $.fn.fadeInText = function (message) {
        this.text(message.toString());
        this.stop().fadeIn("normal");
    }
    //Fade in text
    $.fn.fadeInText = function (message) {
        this.text(message.toString());
        this.stop().fadeIn("normal");
    }

    //Block the div and show loading message
    $.fn.showLoading = function () {
        this.block({
            message: "<div class='blockIcon'></div>",
            css:
            {
                width: '70px',
                textAlign: 'center',
                backgroundColor: 'transparent',
                border: 0,
                cursor: 'wait'
            },
            overlayCSS:
            {
                backgroundColor: '#fff',
                opacity: 0.5
            }
        });
    }

    //Unblock the div
    $.fn.hideLoading = function () {
        this.unblock();
    }
})(jQuery);